//
//  DownTools.m
//  AF下载
//
//  Created by dllo on 15/11/13.
//  Copyright © 2015年 直到世界的尽头. All rights reserved.
//

#import "DownTools.h"

@implementation DownTools

+(void)loadActionWithDownUrl:(NSString *)urlString
            WithProgressView:(UIProgressView *)progressView
{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
    
    NSURL *url = [NSURL URLWithString:urlString];
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
        
        NSURL *documentsDirectoryPath = [NSURL fileURLWithPath:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject]];
        return [documentsDirectoryPath URLByAppendingPathComponent:[response suggestedFilename]];
    } completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
        //NSLog(@"File downloaded to: %@", filePath);
    }];
    
    [downloadTask resume];
    
    [progressView setProgressWithDownloadProgressOfTask:downloadTask animated:YES];
}

@end
